package com.bustle.order;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.bustle.order.data.Account;
import com.bustle.order.data.dao.AccountDao;

@Local
public interface AccountManager
{

  public void createAccount(Account account);
  public Account getByUser(int idUser);

  @Stateless
  public class DefaultAccountManager implements AccountManager
  {

    @PersistenceContext(unitName = "bustle-soa-order-data")
    private EntityManager entityManager;
    @EJB
    private AccountDao accountDao;

    @Override
    public void createAccount(Account account)
    {
      accountDao.create(account);
    }

    @Override
    public Account getByUser(int idUser)
    {
      Account account = null;
      String stringQuery = "SELECT a FROM Account a WHERE user.id = :paramId";
      Query query = entityManager.createQuery(stringQuery);
      query.setParameter("paramId", idUser);
      account = (Account)query.getResultList().get(0);
      return account;
    }

  }

}
